﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;

namespace KSS.Generator.Configuration
{
    public class PropertySettingPropertyDescriptor : PropertyDescriptor
    {
        private PropertySetting _property;

        public PropertySettingPropertyDescriptor(PropertySetting property)
            : base(property.Name, null)
        {
            _property = property;
        }

        public override AttributeCollection Attributes
        {
            get
            {
                return new AttributeCollection(null);
            }
        }

        public override bool CanResetValue(object component)
        {
            return true;
        }

        public override Type ComponentType
        {
            get
            {
                return _property.GetType();
            }
        }
        public override string DisplayName
        {
            get
            {
                return _property.Name;
            }
        }
        public override string Description
        {
            get
            {
                return _property.Description;
            }
        }
        public override object GetValue(object component)
        {
            return _property.Value;
        }

        public override bool IsReadOnly
        {
            get { return false; }
        }
        public override string Name
        {
            get { return _property.Name; }
        }
        public override Type PropertyType
        {
            get
            {
                Type t = ConfigurationHelper.GetDataType(_property.DataType);
                if (t == null)
                {
                    t = ConfigurationHelper.GetDataType(_property.Assembly, _property.DataType);
                }

                return t;
            }
        }

        public override void ResetValue(object component)
        {
            
        }

        public override void SetValue(object component, object value)
        {
            _property.Value = value as string;
        }

        public override bool ShouldSerializeValue(object component)
        {
            return true;
        }
    }
}
